<?php
	// +----------------------------------------------------------------------
	// | 用户预存款记录
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2020 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------
	namespace app\model\ucenter;

	class UserSign extends Common
	{

		protected static $level = [0, 10, 20, 30, 50];

		public function user()
		{
			return $this->belongsTo(User::class);//->field('id,nickname,avatar,avatar_id');
		}

		/**
		 * 根据天数获取下一天的等级
		 * @param $day
		 * @return mixed
		 * @author  : 微尘 <yicmf@qq.com>
		 */
		public static function getLevel($day)
		{
			$value = 0;
			foreach (self::$level as $key => $level) {
				if ($level <= $day) {
					$value = $key;
				};
			}
			return $value + 1;
		}

		protected function setLevelAttr($value, $data)
		{
			return self::getLevel($data['sign_day']);
		}

		/**
		 * 写入修改
		 * @param $data
		 */
		public static function onBeforeInsert($data)
		{
			$data['status'] = 1;
			$data['level'] = '';
			$data['os'] = '';
			$data['ip'] = self::$request->ip();
		}

		protected function setOsAttr($value)
		{
			if ('cli' != PHP_SAPI) {
				return self::$request->server('HTTP_USER_AGENT');
			} else {
				return 'cli';
			}
		}
	}
